<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
$store = Mage::app()->getStore();
$code  = $store->getCode();
    $_productCollection=$this->getLoadedProductCollection();
    $_productCollection->addAttributeToSelect('news_from_date')
        ->addAttributeToSelect('news_to_date');
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
    <?php
        if(($i%6) == 0){
    ?>
        <ul class="products-grid">
    <?php
        }
    ?>
            <li class="item<?php if($i==0): ?> first<?php elseif($i==$_collectionSize-1): ?> last<?php endif; ?>">
                <div class="product-image-area" hover_img="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350) ?>" base_img="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350) ?>" >
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
                    </a>
                    <?php 
                        $top_position = 10;
                        // Get the Special Price
                        $specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice(); 
                        // Get the Special Price FROM date
                        $specialPriceFromDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialFromDate();
                        // Get the Special Price TO date
                        $specialPriceToDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialToDate();
                        // Get Current date
                        $today =  time();
                     
                        if ($specialprice){
                            if($today >= strtotime( $specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)){
                                if(Mage::getStoreConfig("trego_settings/product_label/sale", $code)){
                    ?>
                                <div class="sale-product-icon" style="top: <?php echo $top_position; ?>px"><?php echo $this->__('Sale') ?></div>
                    <?php       
                                $top_position += 30;
                                }
                            }
                        }
                    ?>
                    <?php
                        $now = date("Y-m-d");
                        $newsFrom= substr($_product->getData('news_from_date'),0,10);
                        $newsTo=  substr($_product->getData('news_to_date'),0,10);
                        if ($newsTo != '' || $newsFrom != ''){
                            if (($newsTo != '' && $newsFrom != '' && $now>=$newsFrom && $now<=$newsTo) || ($newsTo == '' && $now >=$newsFrom) || ($newsFrom == '' && $now<=$newsTo))
                            {
                                if(Mage::getStoreConfig("trego_settings/product_label/new", $code)){
                        ?> 
                                <div class="new-product-icon" style="top: <?php echo $top_position; ?>px"><?php echo $this->__('New');?></div>
                        <?php 
                                $top_position += 30;
                                }
                            }
                        }
                    ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                    <?php
                        if(Mage::getStoreConfig("trego_settings/category_grid/hover_button", $code)){    
                    ?>
                    <ul class="cart-button-set">
                        <li class="first"><a href="<?php echo $this->getAddToCartUrl($_product) ?>" class="addtocart" title="<?php echo $this->__('Add to Cart') ?>"><?php echo $this->__('Add to Cart') ?></a></li>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="addtowishlist" title="<?php echo $this->__('Add to Wishlist') ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <li class="last"><a href="<?php echo $this->getAddToCompareUrl($_product) ?>" class="addtocompare" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    </ul>
                    <?php
                        }
                    ?>
                    <div class="clearer"></div>
                </div>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php $i++; ?>
            </li>
    <?php
        if(($i%6) == 0 || $i==$_collectionSize){
    ?>
        </ul>
    <?php
        }
    ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>
        <div class="category-right">
        <?php
            echo $this->getChildHtml("category-right");
        ?>
        </div>
    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
    <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('.products-grid li:nth-child(3n)').addClass('nth-child-3n');
        jQuery('.products-grid li:nth-child(3n+1)').addClass('nth-child-3np1');
        jQuery('.products-grid li:nth-child(2n)').addClass('nth-child-2n');
        jQuery('.products-grid li:nth-child(2n+1)').addClass('nth-child-2np1');
<?php
    if(Mage::getStoreConfig("trego_settings/category_grid/hover_button", $code)){    
?>
        jQuery(".cart-button-set").each(function(){
            var setobj = jQuery(this);
            var minWidth = 16;
            jQuery(this).children("li").css("width", minWidth+"px");
            jQuery(this).children("li").css("overflow", "hidden");
            jQuery(this).children("li").first().css("width", (jQuery(setobj).width()-96)+"px");
            /*
            jQuery(this).children("li").mouseenter(    
                function(){
                    var cur_obj = jQuery(this);
                    jQuery(this).parent().children("li").each(function(){
                        if(jQuery(this) != jQuery(cur_obj))
                            jQuery(this).animate({width: minWidth+"px"}, { queue:false, duration:400 });
                    });
                    jQuery(this).animate({width: (jQuery(setobj).width()-96)+"px"}, { queue:false, duration:400});
                }
            );*/
        });
        jQuery('.category-products .product-image-area').mouseenter(function(){
            jQuery(this).children('.cart-button-set').children("li").css('width', "16px");
            jQuery(this).children('.cart-button-set').children("li").first().css('width', (jQuery(this).children('.cart-button-set').width()-96)+"px");
            jQuery(this).children('.cart-button-set').fadeIn();
        }).mouseleave(function(){
            jQuery(this).children('.cart-button-set').fadeOut();
        });
<?php
    }
    if(Mage::getStoreConfig("trego_settings/category/alternative_image", $code)){    
?>
        jQuery('.category-products .product-image-area').each(function(){
            var inarea = false;
            jQuery(this).mouseenter(function(){
                if(!jQuery(this).parent().parent().parent().parent().parent().hasClass('block') && inarea == false){
                    var hover_img = jQuery(this).attr('hover_img');
                    var base_img = jQuery(this).attr('base_img');
                    var img = jQuery(this).children('a.product-image').children('img');
                    //jQuery(img).attr('src', hover_img);
                    /********************************/
                    var ele = jQuery(img).clone().appendTo(jQuery(img).parent()).css({position:"absolute","top":"0px","left":"0px","z-index":"2"}).attr("src",base_img);
                    jQuery(img).css("z-index",1);
                    jQuery(img).attr('src', hover_img);
                    jQuery(ele).fadeOut(400,function(){jQuery(ele).remove(); action=false});
                }
                inarea = true;
            }).mouseleave(function(){
                if(!jQuery(this).parent().parent().parent().parent().parent().hasClass('block') && inarea == true){
                    var hover_img = jQuery(this).attr('hover_img');
                    var base_img = jQuery(this).attr('base_img');
                    var img = jQuery(this).children('a.product-image').children('img');
                    //jQuery(img).attr('src', base_img);
                    /********************************/
                    var ele = jQuery(img).clone().appendTo(jQuery(img).parent()).css({position:"absolute","top":"0px","left":"0px","z-index":"2"}).attr("src",hover_img);
                    jQuery(img).css("z-index",1);
                    jQuery(img).attr('src', base_img);
                    jQuery(ele).fadeOut(400,function(){jQuery(ele).remove(); action=false});
                }
                inarea = false;
            });
        });
<?php
    }
?>
    });
    </script>
</div>
